package com.attendance.www.hr.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.ReportAttendanceBean;
import com.attendance.www.hr.beans.ReportAttendanceParam;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.dao.mapper.ReportAttendanceExtMapper;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IReportExportService;
import com.attendance.www.hr.service.IReportResultService;

@Service
public class ReportExportServiceImpl implements IReportExportService {
	
	@Resource
	private ReportAttendanceExtMapper reportDao;
	@Resource
	private IReportResultService reportResultService;
	
	private Logger log = Logger.getLogger(ReportExportServiceImpl.class);

	/**
	 * 导出报表
	 */
	@SuppressWarnings("resource")
	@Override
	public String getExportReport(ReportAttendanceParam paramBean) {
			long time = System.currentTimeMillis();
			InputStream inputStream=null;
			Workbook wb = null;
			String strNull=null;
			String url =null;
			String fileurl=null;
			try {
				String readUrl = AppConstant.TEMPLATEFILEREPOSITORY + "/报表记录明细.xlsx";
			    fileurl = AppConstant.REPORTFILEREPOSITORY + "/报表记录明细" + time + ".xlsx";
			    url = AppConstant.REPORT_RESOURCE + "/报表记录明细" + time + ".xlsx";
				File file = new File(readUrl);
				if (!file.exists()) {
					try {
						file.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			    inputStream = new FileInputStream(readUrl);
				if (readUrl.matches("^.+\\.(?i)(xls)$")) {
					wb = new HSSFWorkbook(inputStream);
				} else if (readUrl.matches("^.+\\.(?i)(xlsx)$")) {
					wb = new XSSFWorkbook(inputStream);
				} else {
					throw new ResultFaultException("8_00001");
				}
			} catch (Exception e) {
		
			}
		
			Sheet sheet = wb.getSheetAt(0);
			// 创建单元格样式对象
			XSSFCellStyle ztStyle = (XSSFCellStyle) wb.createCellStyle();
			//日期格式
			XSSFCellStyle dateZtStyle = (XSSFCellStyle) wb.createCellStyle();
			
			// 设置字体样式
			Font ztFont = wb.createFont();
			ztFont.setFontHeightInPoints((short) 9); // 将字体大小设置为18px
			ztFont.setFontName("微软雅黑"); // 将“华文行楷”字体应用到当前单元格上
			ztStyle.setFont(ztFont);
			// 设置边框颜色
			ztStyle.setBorderBottom(CellStyle.BORDER_THIN);
			ztStyle.setBorderTop(CellStyle.BORDER_THIN);
			ztStyle.setBorderLeft(CellStyle.BORDER_THIN);
			ztStyle.setBorderRight(CellStyle.BORDER_THIN);
			
			dateZtStyle.setFont(ztFont);
			// 设置边框颜色
			dateZtStyle.setBorderBottom(CellStyle.BORDER_THIN);
			dateZtStyle.setBorderTop(CellStyle.BORDER_THIN);
			dateZtStyle.setBorderLeft(CellStyle.BORDER_THIN);
			dateZtStyle.setBorderRight(CellStyle.BORDER_THIN);

			// 设置单元格边框颜色
			XSSFColor ztColor = new XSSFColor(java.awt.Color.black);
			ztStyle.setBottomBorderColor(ztColor);
			ztStyle.setTopBorderColor(ztColor);
			ztStyle.setLeftBorderColor(ztColor);
			ztStyle.setRightBorderColor(ztColor);
			
			dateZtStyle.setBottomBorderColor(ztColor);
			dateZtStyle.setTopBorderColor(ztColor);
			dateZtStyle.setLeftBorderColor(ztColor);
			dateZtStyle.setRightBorderColor(ztColor);

			DataFormat fmt = wb.createDataFormat();  
			dateZtStyle.setDataFormat(fmt.getFormat("yyyy-MM-dd"));
			
			List<ReportAttendanceBean> resultList=reportResultService.getReportResultInfoList(paramBean).getReportInfoList();
			for (int i = 0; i < resultList.size(); i++) {
				ReportAttendanceBean reportAttendanceBean = resultList.get(i);
				if (reportAttendanceBean == null) {
					continue;
				}
				Row row = sheet.createRow(i + 3);
				Cell cell = row.createCell(0);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getDeptmentNameOne() != null) {
					cell.setCellValue(reportAttendanceBean.getDeptmentNameOne());
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(1);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getDeptmentNameTwo() != null) {
					cell.setCellValue(reportAttendanceBean.getDeptmentNameTwo());
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(2);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getDeptmentNameThree() != null) {
					cell.setCellValue(reportAttendanceBean.getDeptmentNameThree());
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(3);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getDeptmentNameFour() != null) {
					cell.setCellValue(reportAttendanceBean.getDeptmentNameFour());
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(4);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getDeptmentNameFive() != null) {
					cell.setCellValue(reportAttendanceBean.getDeptmentNameFive());
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(5);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getEmployeeNo() != null) {
					cell.setCellValue(reportAttendanceBean.getEmployeeNo());
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(6);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getChineseName() != null) {
					cell.setCellValue(reportAttendanceBean.getChineseName());
				} else {
					cell.setCellValue(strNull);
				}
				
				cell = row.createCell(7);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getSex() != null) {
					if(reportAttendanceBean.getSex()==1) {
						cell.setCellValue("男");
					}else {
						cell.setCellValue("女");
					}
				} else {
					cell.setCellValue(strNull);
				}
				
				cell = row.createCell(8);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getLevel() != null) {
					cell.setCellValue(reportAttendanceBean.getLevel());
				} else {
					cell.setCellValue(strNull);
				}
				
				cell = row.createCell(9);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRoleName() != null) {
					cell.setCellValue(reportAttendanceBean.getRoleName());
				} else {
					cell.setCellValue(strNull);
				}
				
				cell = row.createCell(10);
//				cell.setCellStyle(ztStyle);
				cell.setCellStyle(dateZtStyle); 
				if (reportAttendanceBean.getEntryDate() != null) {
					java.util.Date date=reportAttendanceBean.getEntryDate(); 
					cell.setCellValue(date);
				} else {
					cell.setCellValue(strNull);
				}
			
				cell = row.createCell(11);
//				cell.setCellStyle(ztStyle);
				cell.setCellStyle(dateZtStyle);
				if (reportAttendanceBean.getEmployeeSeparatedDate() != null) {
					java.util.Date date=reportAttendanceBean.getEmployeeSeparatedDate();  
					cell.setCellValue(date);
				} else {
					cell.setCellValue(strNull);
				}
				cell = row.createCell(12);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRealAttendanceDays() != null) {
					cell.setCellValue(reportAttendanceBean.getRealAttendanceDays());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(13);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getLateCount() != null) {
					cell.setCellValue(reportAttendanceBean.getLateCount());
				} else {
//					cell.setCellValue(strNull);
				}
				//迟到扣款
				cell = row.createCell(14);
				cell.setCellStyle(ztStyle);
				cell.setCellValue(strNull);
				
				cell = row.createCell(15);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getLeaveEarlyCount() != null) {
					cell.setCellValue(reportAttendanceBean.getLeaveEarlyCount());
				} else {
//					cell.setCellValue(strNull);
				}
				//早退扣款
				cell = row.createCell(16);
				cell.setCellStyle(ztStyle);
				cell.setCellValue(strNull);
				
				cell = row.createCell(17);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getAbsentCount() != null) {
					cell.setCellValue(reportAttendanceBean.getAbsentCount());
				} else {
//					cell.setCellValue(strNull);
				}
				//补签
				cell = row.createCell(18);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRetroactiveCount() != null) {
					cell.setCellValue(reportAttendanceBean.getRetroactiveCount());
				} else {
//					cell.setCellValue(strNull);
				}
				
				cell = row.createCell(19);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getOvertimeNomal() != null) {
					cell.setCellValue(reportAttendanceBean.getOvertimeNomal());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(20);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getOvertimeWeekend() != null) {
					cell.setCellValue(reportAttendanceBean.getOvertimeWeekend());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(21);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getOvertimeHoliday() != null) {
					cell.setCellValue(reportAttendanceBean.getOvertimeHoliday());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(22);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestYearUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestYearUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(23);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestTuneUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestTuneUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(24);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestBornUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestBornUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(25);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestCheckUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestCheckUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(26);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestDieUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestDieUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(27);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestMarryUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestMarryUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(28);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestBornAttendUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestBornAttendUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(29);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestSuckleUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestSuckleUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(30);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestIllUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestIllUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(31);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestThingUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getRestThingUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(32);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearLateCount() != null) {
					cell.setCellValue(reportAttendanceBean.getYearLateCount());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(33);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearLeaveEarlyCount() != null) {
					cell.setCellValue(reportAttendanceBean.getYearLeaveEarlyCount());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(34);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearAbsentCount() != null) {
					cell.setCellValue(reportAttendanceBean.getYearAbsentCount());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(35);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearOvertimeNomal() != null) {
					cell.setCellValue(reportAttendanceBean.getYearOvertimeNomal());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(36);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearOvertimeWeekend() != null) {
					cell.setCellValue(reportAttendanceBean.getYearOvertimeWeekend());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(37);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearOvertimeHoliday() != null) {
					cell.setCellValue(reportAttendanceBean.getYearOvertimeHoliday());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(38);
//				cell.setCellStyle(ztStyle);
				cell.setCellStyle(dateZtStyle);
				if (reportAttendanceBean.getYearRestYearBegin() != null) {
					java.util.Date date=reportAttendanceBean.getYearRestYearBegin();  
					cell.setCellValue(date);
				} else {
//					cell.setCellValue(strNull);
				}
				//可享受年假
				cell = row.createCell(39);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestYearAll() != null) {
//					cell.setCellValue(reportAttendanceBean.getYearRestYearAll());
					cell.setCellValue(reportAttendanceBean.getYearRestYearAll().intValue());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(40);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestYearUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestYearUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				//剩余年假
				cell = row.createCell(41);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getRestYearRemain() != null) {
					cell.setCellValue(reportAttendanceBean.getRestYearRemain());
				} else {
//					cell.setCellValue(strNull);
				}
				//去年使用年假天数
				cell = row.createCell(42);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestLastYearUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestLastYearUsed().intValue());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(43);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestLastYearAll() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestLastYearAll().intValue());
				} else {
//					cell.setCellValue(strNull);
				}
				
				cell = row.createCell(44);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestTuneUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestTuneUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(45);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestTuneRemain() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestTuneRemain());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(46);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestBorn() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestBorn());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(47);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestBornUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestBornUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(48);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestBornRemain() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestBornRemain());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(49);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestCheckUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestCheckUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(50);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestDieUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestDieUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(51);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestMarryUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestMarryUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(52);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestBornAttendUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestBornAttendUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(53);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestSuckleUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestSuckleUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(54);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestIllUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestIllUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				cell = row.createCell(55);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getYearRestThingUsed() != null) {
					cell.setCellValue(reportAttendanceBean.getYearRestThingUsed());
				} else {
//					cell.setCellValue(strNull);
				}
				//备注
				cell = row.createCell(56);
				cell.setCellStyle(ztStyle);
				if (reportAttendanceBean.getDescription() != null) {
					cell.setCellValue(reportAttendanceBean.getDescription().replace("<br>", "\n"));
				} else {
//					cell.setCellValue(strNull);
				}
			}

			FileOutputStream outStream;
			try {
				outStream = new FileOutputStream(fileurl);
				try {
					wb.write(outStream);
					outStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return url;
	}

}
